﻿using System;
using System.Collections.Generic;
using System.Linq;
using WebApp.Commons;
using WebApp.Data;

namespace WebApp.Services
{
    public class CustomerService
    {
        public List<tbl_customer> GetCustomers(ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                count = db.tbl_customers.Count();

                return db.tbl_customers.ToList();
            }
        }

        public List<tbl_customer> GetCustomers(int index, int size, string sortColumn, string sortOrder, string keyword,ref int count)
        {
            using (var db = new PAVINGDataContext())
            {
                //var direction = sortOrder.Equals("desc") ? " descending " : "";
                count = db.tbl_customers.Count(u => u.customer_firstname.Contains(keyword));

               // return db.tbl_customers.Where(u => u.customer_firstname.Contains(keyword)).ToList();
                return db.tbl_customers.Where(u => ((string.IsNullOrEmpty(keyword)) || (u.customer_firstname.Contains(keyword)))
                                                    || (u.customer_lastname.Contains(keyword))
                                                    || (u.customer_tel.Contains(keyword))).ToList();
            
            }
        }
        
        public bool UpdateCustomer(tbl_customer model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    if (model.customer_id == 0)
                    {
                        model.customer_create_by = PavingContext.CurrentUserId();
                        model.customer_create_date = DateTime.Now;

                        string newCustomerDigit = (DataProvider.GetLastCustomerId() +1).ToString();
                        int newId;
                        model.customer_id = int.TryParse(newCustomerDigit, out newId) ? newId : 0;
                        
                        db.tbl_customers.InsertOnSubmit(model);
                    }
                    else
                    {
                        tbl_customer customerModel = db.tbl_customers.SingleOrDefault(s => s.customer_id.Equals(model.customer_id));
                        if(customerModel != null)
                        {
                            customerModel.customer_firstname = model.customer_firstname;
                            customerModel.customer_lastname = model.customer_lastname;
                            customerModel.customer_company = model.customer_company;
                            customerModel.customer_email = model.customer_email;
                            customerModel.customer_fax = model.customer_fax;
                            customerModel.customer_address = model.customer_address;
                            customerModel.customer_tel = model.customer_tel;
                            customerModel.customer_type = model.customer_type;

                            customerModel.customer_update_by = PavingContext.CurrentUserId();
                            customerModel.customer_update_date = DateTime.Now;
                        }
                    }
                    db.SubmitChanges();
                    return true;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("UpdateCustomer : " + ex.Message);
                return false;
            }
        }

        public bool DeleteCustomer(tbl_customer model)
        {
            try
            {
                using (var db = new PAVINGDataContext())
                {
                    var selectedModel = db.tbl_customers.SingleOrDefault(s => s.customer_id.Equals(model.customer_id));

                    if (selectedModel != null)
                    {
                        db.tbl_customers.DeleteOnSubmit(selectedModel);
                        db.SubmitChanges();

                        return true;
                    }

                    return false;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("DeleteCustomer : " + ex.Message);
                return false;
            }
        }
    }
}